package cn.change360.newappchildrenenglish;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.activeandroid.query.Select;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import cn.change360.newappchildrenenglish.mode.Columns;
import cn.change360.newappchildrenenglish.mode.Words;
import cn.change360.newappchildrenenglish.utils.FileUtil;
import cn.change360.newappchildrenenglish.widget.WidgetWord;

/*
* 学单词内容
* */

public class WordsShowActivity extends AppCompatActivity implements View.OnClickListener {
    ImageButton topbar_back;
    TextView topbar_title;

    LinearLayout word_box;

    Long columns_id;
    Columns columns;

    List<Words> wordsList;

    ViewPager viewpage;

    List<View> viewList;

    HorizontalScrollView horiz;

    int itemWidth;

    TextView test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_words_show);

        columns_id = getIntent().getLongExtra("columns_id", 0);
        columns = new Select().from(Columns.class).where("column_id=?", columns_id).executeSingle();

        initView();
    }

    private void initView() {
        topbar_back = (ImageButton) findViewById(R.id.topbar_back);
        topbar_title = (TextView) findViewById(R.id.topbar_title);
        topbar_title.setText("学单词之" + columns.getColumn_title());
        topbar_back.setOnClickListener(this);

        viewpage = (ViewPager) findViewById(R.id.viewpage);
        word_box = (LinearLayout) findViewById(R.id.word_box);

        horiz = (HorizontalScrollView) findViewById(R.id.horiz);

        test = (TextView) findViewById(R.id.test);

        getWordItemWidth();

        showData();
    }

    private void showData() {
        wordsList = new Select().from(Words.class).where("word_column_id=" + columns.getId()).execute();
        for (final Words word : wordsList) {
            WidgetWord widgetWord = new WidgetWord(this);
            widgetWord.setImg(new BitmapDrawable(FileUtil.getImageFromAssetsFile(this, "word/" + word.getWord_title() + ".jpg")));
            word_box.addView(widgetWord);
            widgetWord.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    viewpage.setCurrentItem(wordsList.indexOf(word));
                }
            });
        }

        viewList = new ArrayList<>();
        for (int i = 0; i < wordsList.size(); i++) {
            viewList.add(getView(i));
        }

        viewpage.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return viewList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public int getItemPosition(Object object) {
                return super.getItemPosition(object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ((ViewPager) container).addView(viewList.get(position));
                horizScroll();
                return viewList.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                ((ViewPager) container).removeView(viewList.get(position));
            }
        });
    }

    private void horizScroll() {
        test.setText(viewpage.getCurrentItem() + "");
        WidgetWord widgetWord = (WidgetWord) word_box.getChildAt(viewpage.getCurrentItem());
        widgetWord.setThis();
        horiz.scrollTo(itemWidth * viewpage.getCurrentItem(), horiz.getScrollY());
    }

    public View getView(int index) {
        View view;
        TextView word_title;
        TextView word_translation;
        ImageView word_img;
        ImageButton btn_reading, btn_write;

        view = LayoutInflater.from(this).inflate(R.layout.adapter_words_show, null);
        word_title = (TextView) view.findViewById(R.id.word_title);
        word_translation = (TextView) view.findViewById(R.id.word_translation);
        word_img = (ImageView) view.findViewById(R.id.word_img);
        btn_reading = (ImageButton) view.findViewById(R.id.btn_reading);
        btn_write = (ImageButton) view.findViewById(R.id.btn_write);

        word_title.setText(wordsList.get(index).getWord_title().replaceAll("\n", ""));
        word_translation.setText(wordsList.get(index).getWord_translation());
        word_img.setImageDrawable(new BitmapDrawable(FileUtil.getImageFromAssetsFile(this, "word/" + wordsList.get(index).getWord_title() + ".jpg")));

        btn_reading.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                reading();
            }
        });
        btn_write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(WordsShowActivity.this, WordsWriteActivity.class);
                intent.putExtra("word", wordsList.get(viewpage.getCurrentItem()).getWord_title());
                startActivity(intent);
            }
        });

        return view;
    }

    private void reading() {
        AssetManager am = getAssets();
        AssetFileDescriptor afd;
        try {
            afd = am.openFd("raw/" + wordsList.get(viewpage.getCurrentItem()).getWord_title() + ".mp3");
            MediaPlayer mediaPlayer = new MediaPlayer();
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.topbar_back:
                finish();
                break;
        }
    }

    public void getWordItemWidth() {
        int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        WidgetWord widgetWord = new WidgetWord(this);
        widgetWord.measure(w, h);
        int height = widgetWord.getMeasuredHeight();
        int width = widgetWord.getMeasuredWidth();
        itemWidth = width;
    }
}
